package com.hhkj.api.order.common.component.supplier;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.hhkj.admin.order.domain.OrderItem;
import com.hhkj.admin.order.domain.OrderRefundItemAsso;
import com.hhkj.admin.order.service.base.OrderItemService;
import com.hhkj.admin.order.service.extend.OrderRefundItemAssoExtendService;
import com.hhkj.api.order.model.res.ApiOrderItemRes;
import com.hhkj.api.order.model.res.ApiOrderRefundRes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * ApiOrderRefundItemSupplier
 *
 * @author HHKJ-SERVER-ZJ
 * @since 2023/11/3 23:43
 */
@Component
public class ApiOrderRefundSupplier {

    @Autowired
    private OrderRefundItemAssoExtendService refundItemAssoExtendService;
    @Autowired
    private OrderItemService orderItemService;

    /**
     * 补充订单学员信息
     * @param refundList 退款信息
     */
    public void supplyItem(List<ApiOrderRefundRes> refundList) {
        // 2、获取退款学员对应信息列表
        if (CollectionUtil.isNotEmpty(refundList)) {
            List<Long> refundIdList = refundList.stream()
                    .map(ApiOrderRefundRes::getId)
                    .collect(Collectors.toList());
            List<OrderRefundItemAsso> refundItemAssoList
                    = refundItemAssoExtendService.listByRefundIdList(refundIdList);
            List<Long> itemIdList = refundItemAssoList.stream()
                    .map(OrderRefundItemAsso::getOrderItemId)
                    .collect(Collectors.toList());
            // 2.1 获取订单项目id列表
            List<OrderItem> itemList = orderItemService.listByIdList(itemIdList);

            Map<Long, ApiOrderItemRes> itemResByIdMap = itemList.stream()
                    .map(itemPo -> BeanUtil.toBean(itemPo, ApiOrderItemRes.class))
                    .collect(Collectors.toMap(ApiOrderItemRes::getId, Function.identity()));

//            Map<Long, List<Long>> itemIdListByRefundIdMap = refundItemAssoList.stream()
//                    .collect(Collectors.groupingBy(OrderRefundItemAsso::getRefundId,
//                            Collectors.collectingAndThen(Collectors.toList(),
//                                    assoPoList -> assoPoList.stream()
//                                            .map(OrderRefundItemAsso::getRefundId)
//                                            .collect(Collectors.toList()))));
            Map<Long, List<Long>> itemIdListByRefundIdMap = refundItemAssoList.stream()
                    .collect(Collectors.groupingBy(
                            OrderRefundItemAsso::getRefundId, //key
                            Collectors.mapping(OrderRefundItemAsso::getOrderItemId, Collectors.toList()) //value
                    ));

            List<Long> refundItemIdList;
            List<ApiOrderItemRes> refundItemList;
            for (ApiOrderRefundRes result : refundList) {
                if (!itemIdListByRefundIdMap.containsKey(result.getId())) {
                    continue;
                }
                refundItemIdList = itemIdListByRefundIdMap.get(result.getId());
                refundItemList = new ArrayList<>();
                for (Long refundItemId : refundItemIdList) {
                    if (itemResByIdMap.containsKey(refundItemId)) {
                        refundItemList.add(itemResByIdMap.get(refundItemId));
                    }
                }
                result.setItemList(refundItemList);
            }
        }
    }
}
